{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "efcb4c7b-0baf-4b50-ad59-e04e21ecea17",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-07T05:46:39.055980Z",
     "iopub.status.busy": "2022-06-07T05:46:39.055619Z",
     "iopub.status.idle": "2022-06-07T05:46:39.064285Z",
     "shell.execute_reply": "2022-06-07T05:46:39.063501Z",
     "shell.execute_reply.started": "2022-06-07T05:46:39.055916Z"
    }
   },
   "outputs": [],
   "source": [
    "# 导入包\n",
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "149e9932-4c8b-4a85-993d-c628bbba7789",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-07T05:54:05.958468Z",
     "iopub.status.busy": "2022-06-07T05:54:05.958007Z",
     "iopub.status.idle": "2022-06-07T05:54:05.963531Z",
     "shell.execute_reply": "2022-06-07T05:54:05.962756Z",
     "shell.execute_reply.started": "2022-06-07T05:54:05.958424Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'test string\\t data \\tqq'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建测试字符串\n",
    "text = \"test string\\t data \\tqq\"\n",
    "text"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b81ebdc0-3d7d-4d12-aab0-c06bf82f9bee",
   "metadata": {},
   "source": [
    "## 一、分割字符串（split）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eea9ab0e-4255-4918-aca1-37220c937969",
   "metadata": {},
   "source": [
    "### 1.1 直接使用字符串表达正则表达式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "635df27e-ca6a-4f07-af14-b378f2606d1b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-07T05:55:43.191372Z",
     "iopub.status.busy": "2022-06-07T05:55:43.190845Z",
     "iopub.status.idle": "2022-06-07T05:55:43.196106Z",
     "shell.execute_reply": "2022-06-07T05:55:43.195441Z",
     "shell.execute_reply.started": "2022-06-07T05:55:43.191348Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['test', 'string', 'data', 'qq']"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# re.split(正则表达式字符串,要处理的字符串)\n",
    "# \"\\s+\"表示所有空字符，包括空格、制表符号\n",
    "re.split(\"\\s+\",text)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "935e9381-2696-449c-8959-c211ec36ff65",
   "metadata": {},
   "source": [
    "### 1.2 编译生成正则表达式对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "75d6821b-c35d-46c8-9ad2-7d7487e4b5e6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-07T05:57:42.017097Z",
     "iopub.status.busy": "2022-06-07T05:57:42.016705Z",
     "iopub.status.idle": "2022-06-07T05:57:42.023278Z",
     "shell.execute_reply": "2022-06-07T05:57:42.022549Z",
     "shell.execute_reply.started": "2022-06-07T05:57:42.017072Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['test', 'string', 'data', 'qq']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# re.compile(正则表达式字符串)\n",
    "# 把字符串编译生成正则表达式对象，后续可以反复调用\n",
    "res = re.compile(\"\\s+\")\n",
    "# 通过正则表示对象直接调用split函数\n",
    "res.split(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "27297d82-e7f1-4a0a-b662-604c26ffcdcb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-07T05:58:36.403678Z",
     "iopub.status.busy": "2022-06-07T05:58:36.403352Z",
     "iopub.status.idle": "2022-06-07T05:58:36.408684Z",
     "shell.execute_reply": "2022-06-07T05:58:36.407853Z",
     "shell.execute_reply.started": "2022-06-07T05:58:36.403653Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['test', 'string', 'data', 'qq']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 在re.split函数中调用正则表达式对象\n",
    "re.split(res, text)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f11dc1d-6bb5-4604-a42d-b17aa8a153ff",
   "metadata": {},
   "source": [
    "## 二、模式匹配"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e8c0471b-6a6d-4f52-8bae-af65b62b80ff",
   "metadata": {},
   "source": [
    "### 2.1 多匹配"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "a89ac549-b085-408b-920a-9c1ecc041220",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-07T06:00:23.913899Z",
     "iopub.status.busy": "2022-06-07T06:00:23.913521Z",
     "iopub.status.idle": "2022-06-07T06:00:23.920563Z",
     "shell.execute_reply": "2022-06-07T06:00:23.919284Z",
     "shell.execute_reply.started": "2022-06-07T06:00:23.913871Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[' ', '\\t ', ' \\t']"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查找text中所有能与res匹配的字符串\n",
    "# 通过re调用findall函数\n",
    "re.findall(res, text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "b8eb0bb5-ddf0-4c40-8ca7-2f17dcc4c160",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-07T06:00:39.513531Z",
     "iopub.status.busy": "2022-06-07T06:00:39.513067Z",
     "iopub.status.idle": "2022-06-07T06:00:39.519495Z",
     "shell.execute_reply": "2022-06-07T06:00:39.518416Z",
     "shell.execute_reply.started": "2022-06-07T06:00:39.513503Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[' ', '\\t ', ' \\t']"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查找text中所有能与res匹配的字符串\n",
    "# 通过正则表达式对象调用findall函数\n",
    "res.findall(text)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71b7a5c9-27c4-41f4-b715-66908abe041b",
   "metadata": {},
   "source": [
    "### 2.2 单匹配"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "f39931a1-eac5-4c68-8704-df0a60b1d4f0",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-07T06:10:34.072039Z",
     "iopub.status.busy": "2022-06-07T06:10:34.071482Z",
     "iopub.status.idle": "2022-06-07T06:10:34.077169Z",
     "shell.execute_reply": "2022-06-07T06:10:34.076487Z",
     "shell.execute_reply.started": "2022-06-07T06:10:34.072013Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 1), match='t'>"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 从第0个位置开始匹配给定正则表达式\n",
    "# 匹配字符串必须是从text的第0个位置开始\n",
    "# 匹配成功返回match对象，否则返回None\n",
    "re.match(\"t\",text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "925aa09f-fa0e-4134-8fb1-e19551b79c15",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-07T06:17:40.696823Z",
     "iopub.status.busy": "2022-06-07T06:17:40.696137Z",
     "iopub.status.idle": "2022-06-07T06:17:40.702371Z",
     "shell.execute_reply": "2022-06-07T06:17:40.701561Z",
     "shell.execute_reply.started": "2022-06-07T06:17:40.696797Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(13, 14), match='d'>"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 从第0个位置开始匹配给定正则表达式\n",
    "# 匹配字符串可以再text的任意位置\n",
    "# 返回第一个匹配成功的位置，否则返回None\n",
    "re.search(\"d\",text)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20b4c211-0ba1-493d-9d16-96b38fa50686",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-07T06:20:04.110808Z",
     "iopub.status.busy": "2022-06-07T06:20:04.110490Z",
     "iopub.status.idle": "2022-06-07T06:20:04.114482Z",
     "shell.execute_reply": "2022-06-07T06:20:04.113486Z",
     "shell.execute_reply.started": "2022-06-07T06:20:04.110784Z"
    },
    "tags": []
   },
   "source": [
    "### 2.3 匹配替换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "d415c2b7-c142-4aee-8459-0d2e56c05715",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-07T06:01:51.049803Z",
     "iopub.status.busy": "2022-06-07T06:01:51.049490Z",
     "iopub.status.idle": "2022-06-07T06:01:51.056806Z",
     "shell.execute_reply": "2022-06-07T06:01:51.055199Z",
     "shell.execute_reply.started": "2022-06-07T06:01:51.049780Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'testTTTstringTTTdataTTTqq'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 把text中所有匹配res的字符串都替换成TTT\n",
    "re.sub(res,\"TTT\",text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "5ba72a5e-7e2f-4eaf-a624-95d0d0e02054",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-07T06:21:39.584545Z",
     "iopub.status.busy": "2022-06-07T06:21:39.584189Z",
     "iopub.status.idle": "2022-06-07T06:21:39.589853Z",
     "shell.execute_reply": "2022-06-07T06:21:39.588904Z",
     "shell.execute_reply.started": "2022-06-07T06:21:39.584518Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('testTTTstringTTTdata \\tqq', 2)"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 把text中前n个匹配res的字符串都替换成TTT\n",
    "# 这里n=2\n",
    "re.subn(res,\"TTT\",text, 2)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
